package com.hc.demo.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.common.utils.QRCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/qrcode")
public class QRCodeController {

    /**
     * 根据 content 生成二维码
     * 测试：http://localhost/blog/portals/qrcode/getQRCode?content=www.hcshow.online&logoUrl=http://pic.51yuansu.com/pic3/cover/03/84/85/5c11a40a32429_610.jpg
     *
     * @param content
     * @param width
     * @param height
     * @return
     */
    @GetMapping("/getQRCodeBase64")
    public Result getQRCode(@RequestParam("content") String content,
                            @RequestParam(value = "logoUrl", required = false) String logoUrl,
                            @RequestParam(value = "width", required = false) Integer width,
                            @RequestParam(value = "height", required = false) Integer height) {
        String base64QRCode = QRCodeUtil.getBase64QRCode(content, logoUrl);
        System.out.println(base64QRCode);
        return ResultUtil.success().setData(base64QRCode);
    }

    /**
     * 根据 content 生成二维码
     * 测试：http://localhost/blog/portals/qrcode/getQRCode?content=www.hcshow.online
     */
    @GetMapping(value = "/getQRCode")
    public void getQRCode(HttpServletResponse response,
                          @RequestParam("content") String content,
                          @RequestParam(value = "logoUrl", required = false) String logoUrl) throws Exception {
        try (ServletOutputStream stream = response.getOutputStream();) {
            QRCodeUtil.getQRCode(content, logoUrl, stream);
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

}
